package com.xk._01基础篇._12堆.heap;

import com.xk._01基础篇._06二叉搜索树.printer.BinaryTreeInfo;

import java.util.Comparator;

/**
 * @description: 二叉堆 (大顶堆)
 * @author: xu
 * @date: 2022/9/28 23:46
 */
public class BinaryHeap<E> extends AbstractHeap<E> implements BinaryTreeInfo {
    private E[] elements;
    private static final int DEFAULT_CAPACITY = 10;

    public BinaryHeap(){
        this(null, null);
    }
    public BinaryHeap(Comparator<E> comparator){
        this(null, comparator);
    }
    public BinaryHeap(E[] elements){
        this(elements, null);
    }

    public BinaryHeap(E[] elements, Comparator<E> comparator){
        super(comparator);
        if (elements == null || elements.length == 0){
            this.elements = (E[]) new Object[DEFAULT_CAPACITY];
        } else {
            size = elements.length;
            int capacity = Math.max(DEFAULT_CAPACITY, elements.length);
            this.elements = (E[]) new Object[capacity];
            for (int i = 0; i < elements.length; i++) {
                this.elements[i] = elements[i];
            }
            heapify();
        }
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            elements[i] = null;
        }
        size = 0;
    }

    @Override
    public void add(E element) {
        elementNotNullCheck(element); // 对element判空
        ensureCapacity(size + 1);
        elements[size++] = element;
        siftUp(size - 1);
    }

    @Override
    public E get() {
        emptyCheck();
        return elements[0];
    }

    @Override
    public E remove() {
        emptyCheck();

        int lastIndex = --size;
        E root = elements[0];
        elements[0] = elements[lastIndex];
        elements[lastIndex] = null;
        siftDown(0);
        return root;
    }

    @Override
    public E replace(E element) {
        elementNotNullCheck(element);
        if (size == 0){
            elements[0] = element;
            size++;
            return null;
        }
        E root = elements[0];
        elements[0] = element;
        siftDown(0);
        return root;
    }

    /**
     * 批量建堆
     */
    private void heapify() {
        // 1.自上而下的上滤
        //for (int i = 1; i < size; i++) {
        //    siftUp(i);
        //}

        // 2.自下而上的下滤
        for (int i = (size >> 1) - 1; i >= 0; i--) {
            siftDown(i);
        }
    }

    /**
     * 让index位置的元素下滤
     * @param index
     */
    private void siftDown(int index) {
        E element = elements[index];
        // 必须保证index位置是非叶子节点
        // 第一个叶子结点的索引 == 叶子结点的数量 = floor(n/2)
        int half = size >> 1;
        while (index < half) {
            // index 的节点有2种情况
            // 1.只有左子节点
            // 2.同时有左右子节点

            // 默认为左子节点跟它进行比较
            int childIndex = (index << 1) + 1;
            E child = elements[childIndex];

            // 右子节点
            int rightIndex = childIndex + 1;
            // 选出左右子节点最大的那个
            if (rightIndex < size && compare(child, elements[rightIndex]) < 0){
                child = elements[childIndex = rightIndex];
            }
            if (compare(element, child) >= 0) break;

            // 将子节点存放到index位置
            elements[index] = child;
            // 重新设置 index
            index = childIndex;
        }
        elements[index] = element;
    }

    /**
     * 让index位置的元素上滤
     * @param index
     */
    private void siftUp(int index){
        //while (index > 0) {
        //    int parentIndex = (index - 1) >> 1;
        //    if (compare(elements[index], elements[parentIndex]) <= 0) break;
        //    // 交换 index、parentIndex 位置的内容
        //    E tmp = elements[index];
        //    elements[index] = elements[parentIndex];
        //    elements[parentIndex] = tmp;
        //    // 重新赋值 index
        //    index = parentIndex;
        //}

        E element = elements[index];
        while (index > 0) {
            // 获取父元素
            int parentIndex = (index - 1) >> 1;
            E parent = elements[parentIndex];
            if (compare(element, parent) <= 0) break;

            // 将父元素存储在index位置
            elements[index] = parent;
            // 重新设置 index
            index = parentIndex;
        }
        elements[index] = element;
    }

    /**
     * 保证要有capacity的容量
     * @param capacity
     */
    private void ensureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if (oldCapacity >= capacity) return;
        //新容量为旧容量的1.5倍
        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }

    private void emptyCheck(){
        if (size == 0)
            throw new IndexOutOfBoundsException("Heap is empty!");
    }

    private void elementNotNullCheck (E element) {
        if (element == null)
            throw new IllegalArgumentException("element must not be null");
    }

    @Override
    public Object root() {
        return 0;
    }

    @Override
    public Object left(Object node) {
        Integer index = ((int) node << 1) + 1;
        return index >= size ? null : index;
    }

    @Override
    public Object right(Object node) {
        Integer index = ((int) node << 1) + 2;
        return index >= size ? null : index;
    }

    @Override
    public Object string(Object node) {
        return elements[(int) node];
    }
}